package com.airnewslinkosky.ticket.ui.flight;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.model.FlightInfo;
import com.airnewslinkosky.ticket.model.MileagerInfo;
import com.airnewslinkosky.ticket.model.Passenger;
import com.airnewslinkosky.ticket.model.SearchFlight;
import com.airnewslinkosky.ticket.pay.util.BaseHelper;
import com.airnewslinkosky.ticket.sql.FavoriteInfoDB;
import com.airnewslinkosky.ticket.ui.BaseActivity;
import com.airnewslinkosky.ticket.util.FlyUtil;
import com.mobclick.android.MobclickAgent;

public class MileagerInfoEdit extends BaseActivity implements
		AdapterView.OnItemClickListener {

	public  final int DIALOG_SINGLE_CHOICE = 9;
	public static Passenger passenger;//本页的登机人信息
	public static FlightInfo flight;//去程
	public static FlightInfo returnFlight;//回程
	public static SearchFlight searchFlight;
	public static String fromAction;//来自那个action
	private Button backBut;
	public SimpleAdapter sa;
	public ListView lv;
	public Button mBtnBack,mBtnNext,mileagerSaveBut;
	public static List<Map<String, String>> data = new ArrayList<Map<String, String>>();

	private ProgressDialog progressDialog = null;
	//证件类型,生日
	private TextView mileagerCardAirline;
	private LinearLayout mileagerCardAirlineLay;
	
	private int postion=0;
    
    //登机人,证件号码,手机号码
    private EditText mileagerCardNumber;
    private Toast toast;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.fly_mileager_info);
		// 删除窗口背景
        getWindow().setBackgroundDrawable(null);
		activityList.add(this);
		
		mileagerCardAirlineLay = (LinearLayout)findViewById(R.id.mileagerCardAirlineLay);
		
		mileagerCardAirline =  (TextView)findViewById(R.id.mileagerCardAirline);
		

		mileagerCardNumber = (EditText)findViewById(R.id.mileagerCardNumber);
        
        int position = getIntent().getIntExtra("position",-1);
		if(position!=-1){
				Map<String, String> map = data.get(position);
				mileagerCardAirline.setText(map.get("mileagerCardAirline"));
				mileagerCardNumber.setText(map.get("mileagerCardNumber"));
		}
		
		mileagerSaveBut = (Button)findViewById(R.id.mileagerSaveBut);
		backBut = (Button)findViewById(R.id.backBut);
		
		OnClickListener b_ocl= new OnClickListener() { 
	    	public void onClick(View v) { 
	    		switch (v.getId()) {
		    		case R.id.backBut:
		    			Intent intent3 = new Intent();
		    			intent3.setClass(MileagerInfoEdit.this, MileagerInfoList.class);
		    			MileagerInfoList.searchFlight = searchFlight;
		    			MileagerInfoList.flight = flight;
		    			MileagerInfoList.returnFlight = returnFlight;
		    			MileagerInfoList.passenger = passenger;
		    			startActivity(intent3);
		    			break;
		    		case R.id.mileagerCardAirlineLay:
		    			showDialog(DIALOG_SINGLE_CHOICE);
		    			break;
		    		case R.id.mileagerSaveBut:
		    			if ("".equals(mileagerCardAirline.getText().toString()) ||  "".equals(mileagerCardNumber.getText().toString())){
		    				BaseHelper.showDialog(MileagerInfoEdit.this, "提示", "请填写完整信息",
		    						R.drawable.infoicon);
		    				return;
		    			}
		    			
		    			HashMap<String, String> map = new HashMap<String, String>();
		    			map.put("mileagerCardAirline", mileagerCardAirline.getText().toString());
		    			map.put("mileagerCardAirlineNo", FlyUtil.getAirlineKey(mileagerCardAirline.getText().toString()));
		    			map.put("mileagerCardNumberStr", "卡号：" + mileagerCardNumber.getText().toString());
		    			map.put("mileagerCardNumber", mileagerCardNumber.getText().toString());
		    			for (int i = 0; i < data.size(); i++) {
		    				Map m = data.get(i);
		    				if (map.get("mileagerCardAirline").equals(m.get("mileagerCardAirline"))
		    						&& map.get("mileagerCardNumber").equals(m.get("mileagerCardNumber"))) {
		    					toast = Toast.makeText(MileagerInfoEdit.this, "已添加该常旅卡信息", Toast.LENGTH_LONG);
			    				toast.setGravity(Gravity.CENTER, 0, 0);
			    				toast.show();
		    					return;
		    				}
		    			}
		    			
		    			data.add(map);
		    			
		    			MileagerInfo mileagerInfo = new MileagerInfo();
		    			mileagerInfo.setMileagerCardAirline( mileagerCardAirline.getText().toString());
		    			mileagerInfo.setMileagerCardNumber(mileagerCardNumber.getText().toString());
		    			mileagerInfo.setMileagerInfo("");
		    			mileagerInfo.setMileagerNO("");
		    			mileagerInfo.setPassengerNO("");
		    			FavoriteInfoDB.setMileagerInfo(MileagerInfoEdit.this, mileagerInfo);
		    			
		    			if(fromAction.equals("ADD")){
			    			Intent intent = new Intent();
							intent.setClass(MileagerInfoEdit.this, PassengerAdd.class);
							PassengerAdd.flight = flight;
							PassengerAdd.returnFlight = returnFlight;
							PassengerAdd.searchFlight = searchFlight;
							PassengerAdd.passenger = passenger;
							PassengerAdd.mileagerInfoList.clear();
							PassengerAdd.mileagerInfoList = data;
							startActivity(intent);
		    			} else {
		    				Intent intent = new Intent();
							intent.setClass(MileagerInfoEdit.this, PassengerEdit.class);
							PassengerEdit.flight = flight;
							PassengerEdit.returnFlight = returnFlight;
							PassengerEdit.searchFlight = searchFlight;
							PassengerEdit.passenger = passenger;
							PassengerEdit.mileagerInfoList.clear();
							PassengerEdit.mileagerInfoList = data;
							startActivity(intent);
		    			}
		    			break;
	    		}
	    	}
		};
		
		mileagerCardAirlineLay.setOnClickListener(b_ocl);
		
		mileagerSaveBut.setOnClickListener(b_ocl);
		backBut.setOnClickListener(b_ocl);
	}
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_SINGLE_CHOICE:
            return new AlertDialog.Builder(MileagerInfoEdit.this)
                .setIcon(R.drawable.ic_menu_more)
                .setTitle("请选择")
                .setSingleChoiceItems(R.array.airlineList, 0, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    	postion = whichButton;
                    	String[] arrs = getResources().getStringArray(R.array.airlineList);
                    	mileagerCardAirline.setText(arrs[whichButton]);
                    	dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
               .create();
		    default:
				return null;
		}
	}
	
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
}
